package com.wwwarehouse.resource_center.adapter.createobject;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.SelectionUnitBean;
import com.wwwarehouse.resource_center.eventbus_event.ChooseUnitEvent;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/9/14.
 *         <p/>
 */

public class UnitAdapter extends RecyclerView.Adapter<UnitAdapter.ViewHolder> {

    private Context mContext = null;
    private int mHeight = 0;
    private int mWidth = 0;
    private List<SelectionUnitBean.EmptyIdentifierBean> tagList = new ArrayList<>();
    private String unitUkid;
    private List<SelectionUnitBean.EmptyIdentifierBean> selectList;
    private boolean isSelected = false;
    private int SIX = 6;   //最小类目级别

    public UnitAdapter(Context context, List<SelectionUnitBean.EmptyIdentifierBean> list, int height, int Width, String unitUkid) {
        this.mContext = context;
        this.mHeight = height;
        this.mWidth = Width;
        this.tagList = list;
        this.unitUkid = unitUkid;
        this.selectList = new ArrayList<>();

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_goods_unit_adapter, parent, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.ll.getLayoutParams();
        params.width = (mWidth - ConvertUtils.dip2px(mContext, 14 * 5)) / 4;//设置当前控件布局的宽度和margin
        params.height = params.width;
        holder.ll.setLayoutParams(params);
        try {
            holder.unit_name.setText(tagList.get(position).getUnitName());
            if (!TextUtils.isEmpty(tagList.get(position).getUnitCharacter())){
                holder.unit_character.setText("("+tagList.get(position).getUnitCharacter()+")");
            }
            holder.itemView.setTag(tagList.get(position));
            if (unitUkid!=null&&unitUkid.equals(tagList.get(position).getUnitUkid())) {
                holder.ll.setSelected(true);
                holder.unit_name.setSelected(true);
                holder.unit_character.setSelected(true);

            } else {
                holder.ll.setSelected(false);
                holder.unit_name.setSelected(false);
                holder.unit_character.setSelected(false);
            }

            holder.ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SelectionUnitBean.EmptyIdentifierBean mBean = tagList.get(position);
                    EventBus.getDefault().post(new ChooseUnitEvent(mBean));
                }
            });
        }  catch (Exception e) {
        }
    }

    @Override
    public int getItemCount() {
        return tagList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
         TextView unit_name;
         TextView unit_character;
        LinearLayout  ll ;
        public ViewHolder(View view) {
            super(view);
            unit_name = (TextView) view.findViewById(R.id.unit_name);
            unit_character = (TextView) view.findViewById(R.id.unit_character);
            ll = (LinearLayout) view.findViewById(R.id.ll);
        }
    }
}
